package io.modifier.basic.compose.widget.privacypolicy

import android.app.ActivityManager
import android.content.Context
import android.content.res.Configuration
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.wquick.base.R
import io.modifier.basic.compose.hooks.data.useState
import io.modifier.basic.compose.hooks.time.useInterval
import io.modifier.basic.compose.theme.AppTheme
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.compose.widget.comm.AppBtn
import io.modifier.basic.compose.widget.comm.CenterRow
import io.modifier.basic.compose.widget.comm.HS
import io.modifier.basic.compose.widget.comm.TextBtn
import io.modifier.basic.compose.widget.comm.V
import io.modifier.basic.compose.widget.weui.core.ui.components.bottomPopup.WeBottomPopup
import kotlin.time.Duration.Companion.seconds

//<editor-fold desc="preview">

@Composable
private fun PreviewT() {
    AppTheme {
        Column(Modifier, verticalArrangement = Arrangement.spacedBy(15.dp)) {
            val privacyPolicyRevertState = rememberPrivacyPolicyRevertState()

            TextBtn("撤回隐私协议") {
                privacyPolicyRevertState.show()
            }
        }
    }
}

@Preview(backgroundColor = 0xFFf5f5f5, showBackground = true, showSystemUi = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Composable
private fun PreviewLightT() {
    PreviewT()
}

//@Preview(backgroundColor = 0xFF121212, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun PreviewDarkT() {
    PreviewT()
}

//</editor-fold>

class PrivacyPolicyRevertState {

    internal var visible by mutableStateOf(false)

    fun show() {
        visible = true
    }

    fun hide() {
        visible = false
    }
}

private fun Context.clearApplicationUserData(): Boolean {
    val activityManager = applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    return activityManager.clearApplicationUserData()
}

@Composable
fun rememberPrivacyPolicyRevertState(afterOk: () -> Unit = {}): PrivacyPolicyRevertState {

    val ctx = LocalContext.current

    val state = remember { PrivacyPolicyRevertState() }

    if (state.visible) {
        PrivacyPolicyRevert(onOkClick = {
            ctx.clearApplicationUserData()
            state.hide()
        }, onCancelClick = {
            state.hide()
        })
    }

    return state
}

/**
 * 用户协议和隐私政策组件
 */
@Composable
private fun PrivacyPolicyRevert(
    onOkClick: (() -> Unit)? = null,
    onCancelClick: (() -> Unit)? = null,
) {
    val defMs = 5
    val (leftMs, setLeftMs) = useState(defMs)
    val (okDisable, setOkDisable) = useState(true)

    if (okDisable) {
        useInterval(optionsOf = {
            period = 1.seconds
        }, ready = true) {
            if (leftMs > 0) {
                setLeftMs(leftMs - 1)
                setOkDisable(true)
            } else {
                setLeftMs(defMs)
                setOkDisable(false)
            }
        }
    }

    WeBottomPopup(true, title = stringResource(R.string.fhad_revoke_agreement_title), onClose = { onCancelClick?.invoke() }) {
        V(20)

        Text(stringResource(R.string.fhad_revoke_agreement_1), style = AppThemeHolder.textStyle.content)

        V(20)

        CenterRow {
            AppBtn(text = "取消", modifier = Modifier.width(120.dp), cornerRadius = 25) {
                onCancelClick?.invoke()
            }
            HS()
            AppBtn(
                text = "确定${if (okDisable) "(${leftMs}s)" else ""}", active = okDisable.not(), modifier = Modifier.width(120.dp),
                cornerRadius = 25
            ) {
                if (okDisable) return@AppBtn
                onOkClick?.invoke()
            }
        }

        V(20)
    }
}